/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://github.com/payara/Payara/blob/main/LICENSE.txt
 * See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at legal/OPEN-SOURCE-LICENSE.txt.
 *
 * GPL Classpath Exception:
 * The Payara Foundation designates this particular file as subject to the "Classpath"
 * exception as provided by the Payara Foundation in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package fish.payara.microprofile.faulttolerance.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Method;
import java.time.temporal.ChronoUnit;

import org.eclipse.microprofile.faulttolerance.Asynchronous;
import org.eclipse.microprofile.faulttolerance.Retry;
import org.eclipse.microprofile.faulttolerance.Timeout;
import org.junit.Test;

import fish.payara.microprofile.faulttolerance.FaultToleranceConfig;
import fish.payara.microprofile.faulttolerance.policy.StaticAnalysisContext;
import fish.payara.microprofile.faulttolerance.test.ConfigOverrides;
import fish.payara.microprofile.faulttolerance.test.TestUtils;

/**
 * Tests that the priority of different override levels is correct.
 * 
 * @author Jan Bernitt
 */
@Retry(delayUnit = ChronoUnit.MINUTES)
public class ConfigOverridePriorityTest {

    private ConfigOverrides overrides = new ConfigOverrides();
    private BindableFaultToleranceConfig configFactory = new BindableFaultToleranceConfig(overrides, null);

    @Test
    public void onlyMethodLevelAnnotationPresent() {
        Method annotatedMethod = TestUtils.getAnnotatedMethod();
        FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
        Timeout timeout = config.getAnnotation(Timeout.class);
        assertEquals(42L, config.value(timeout));

        overrides.override(Timeout.class, "value", 11L);
        assertEquals("should be: global override effective", 11L, config.value(timeout));

        overrides.override(getClass(), Timeout.class, "value", 22L);
        assertEquals("should be: class level override ineffective because no class level annotation present",
                11L, config.value(timeout));

        // do method level override
        overrides.override(annotatedMethod, Timeout.class, "value", 33L);
        assertEquals("should be: method level override effective", 33L, config.value(timeout));
    }

    @Timeout(value = 42L)
    public String onlyMethodLevelAnnotationPresent_Method() {
        return "test";
    }

    @Test
    public void methodAndClassLevelAnnotationPresent() {
        Method annotatedMethod = TestUtils.getAnnotatedMethod();
        FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
        Retry retry = config.getAnnotation(Retry.class);
        assertEquals(ChronoUnit.WEEKS, config.delayUnit(retry));

        overrides.override(Retry.class, "delayUnit", ChronoUnit.ERAS);
        assertEquals("should be: global override effective", ChronoUnit.ERAS, config.delayUnit(retry));

        overrides.override(getClass(), Retry.class, "delayUnit", ChronoUnit.MICROS);
        assertEquals("should be: class level annotation present but override ineffective because method level annotation is present",
                ChronoUnit.ERAS, config.delayUnit(retry));

        overrides.override(annotatedMethod, Retry.class, "delayUnit", ChronoUnit.YEARS);
        assertEquals("should be: method level override effective", ChronoUnit.YEARS, config.delayUnit(retry));
    }

    @Retry(delayUnit = ChronoUnit.WEEKS)
    public String methodAndClassLevelAnnotationPresent_Method() {
        return "test";
    }

    @Test
    public void onlyClassLevelAnnotationPresent() {
        Method annotatedMethod = TestUtils.getAnnotatedMethod();
        FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
        Retry annotation = config.getAnnotation(Retry.class);
        assertEquals(ChronoUnit.MINUTES, config.delayUnit(annotation));

        overrides.override(Retry.class, "delayUnit", ChronoUnit.CENTURIES);
        assertEquals("should be: global override effective", ChronoUnit.CENTURIES, config.delayUnit(annotation));

        overrides.override(getClass(), Retry.class, "delayUnit", ChronoUnit.DAYS);
        assertEquals("should be: class level override effective because annotation present on class level",
                ChronoUnit.DAYS, config.delayUnit(annotation));

        overrides.override(annotatedMethod, Retry.class, "delayUnit", ChronoUnit.HOURS);
        assertEquals("should be: method level override ineffective because no method level annotation present",
                ChronoUnit.DAYS, config.delayUnit(annotation));
    }

    public String onlyClassLevelAnnotationPresent_Method() {
        return "test";
    }

    @Test
    public void enabledIsIndependentOfPresentAnnotations() {
        Method annotatedMethod = TestUtils.getAnnotatedMethod();
        FaultToleranceConfig config = configFactory.bindTo(new StaticAnalysisContext(getClass(), annotatedMethod));
        assertTrue("should be: present annotation is enabled by default", config.isEnabled(Asynchronous.class));
        assertTrue("should be: not present annotation is enabled by default", config.isEnabled(Retry.class));

        overrides.override(Asynchronous.class, "enabled", false);
        overrides.override(Retry.class, "enabled", false);
        assertFalse("should be: global override for present annotation has effect", config.isEnabled(Asynchronous.class));
        assertFalse("should be: global override for not present annotation has effect", config.isEnabled(Retry.class));

        overrides.override(getClass(), Asynchronous.class, "enabled", true);
        overrides.override(getClass(), Retry.class, "enabled", true);
        assertTrue("should be: class level override for present annotation has effect", config.isEnabled(Asynchronous.class));
        assertTrue("should be: class level override for not present annotation has effect", config.isEnabled(Retry.class));

        overrides.override(annotatedMethod, Asynchronous.class, "enabled", false);
        overrides.override(annotatedMethod, Retry.class, "enabled", false);
        assertFalse("should be: method level override for present annotation has effect", config.isEnabled(Asynchronous.class));
        assertFalse("should be: method level override for not present annotation has effect", config.isEnabled(Retry.class));
    }

    @Asynchronous
    public String enabledIsIndependentOfPresentAnnotations_Method() {
        return "test";
    }
}
